package com.example.testrssparser;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.res.AssetManager;
import android.os.Environment;
import android.util.Log;

public class ListDataManager {

	File card;
	String file_name;

	public ListDataManager(File card, String file_name) {
		this.card = card;
		this.file_name = file_name;
	}

	public boolean hasCard() {
		String state = Environment.getExternalStorageState();
		return Environment.MEDIA_MOUNTED.equals(state);
	}

	public boolean hasFile() {
		File file = new File(card, file_name);
		return file.exists();
	}

	public void createFile(InputStream is) throws CardException {

		File file = new File(card, file_name);
		if (file.exists()) {
			file.delete();
		}
		BufferedInputStream in = null;
		BufferedOutputStream out = null;
		try {
			in = new BufferedInputStream(is);
			out = new BufferedOutputStream(new FileOutputStream(file));
			int b;
			while ((b = in.read()) != -1) {
				out.write(b);
			}
		} catch (FileNotFoundException e) {
			throw new CardException("ListDataManager::createFile FileNotFoundException", e);
		} catch (IOException e) {
			throw new CardException("ListDataManager::createFile IOException", e);
		} finally {
			try {
				if (in != null) {
					in.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				throw new CardException("ListDataManager::createFile IOException", e);
			}
		}
	}

	public InputStream readFile() throws CardException {
		File file = new File(card, file_name);
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			throw new CardException("ListDataManager::readFile FileNotFoundException", e);
		}
		return fis;
	}

	public List<String> fileToList() throws CardException {
		List<String> res = new ArrayList<String>();
		File file = new File(card, file_name);
		BufferedReader input = null;
		try {
			input = new BufferedReader(new InputStreamReader(
					new FileInputStream(file)));
			
			String strLine;
			while ((strLine = input.readLine()) != null) {
				res.add(strLine);
			}

		} catch (FileNotFoundException e) {
			throw new CardException("ListDataManager::fileToList FileNotFoundException", e);
		} catch (IOException e) {
			throw new CardException("ListDataManager::fileToList IOException", e);
		} finally {
			try {
				if (input != null) {
					input.close();
				}
			} catch (IOException e) {
				throw new CardException("ListDataManager::createFile IOException", e);
			}
		}

		return res;
	}
}
